package com.cx.bank.manager;

import com.cx.bank.dao.BankDaoImpl;
import com.cx.bank.dao.BankDaoInterface;
import com.cx.bank.factory.UserDaoFactory;
import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;
import com.cx.bank.util.AccountOverDrawnException;
import com.cx.bank.util.InvalidDepositException;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

//在getInstance上加锁，每次只能有一个对象拿到
public class ManagerImpl implements managerInterface {
    private static ManagerImpl instance = null;
    BankDaoInterface userDao = null;
    private ManagerImpl(){
        UserDaoFactory instance1= null;
        try {
            instance1 = UserDaoFactory.getInstance();
            userDao = instance1.createUserDao();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    加锁
    public static synchronized ManagerImpl getInstance(){
        if(instance == null){
            instance = new ManagerImpl();
        }
        return instance;
    }
    /*
     * 方法名：register
     * 完成功能：注册
     */
    @Override
    public boolean register(String username, String password){
        String filePath = "D:\\Code\\Java_Code\\practicalTraining\\File\\"+username+".properties";
        BankDaoInterface bankDaoInterface = BankDaoImpl.getInstance();
        File file = new File(filePath);
        if(file.exists()){
            System.out.println("用户已存在");
            return false;
        }
        String s = MD5(password);
        UserBean userBean = new UserBean(username,s);
//        bankDaoInterface.insertUser(userBean,username);
        userDao.insertUser(userBean,username);
        return true;

    }
    /*
     * 方法名：login
     * 完成功能：登录
     */
    @Override
    public boolean login(String username, String password) {
        BankDaoInterface bankDaoInterface = BankDaoImpl.getInstance();
        UserBean userBean = new UserBean();
//        userBean = bankDaoInterface.findUserByName(username);
        userBean = userDao.findUserByName(username);
        String s = MD5(password);
        if(username.equals(userBean.getUsername())&&s.equals(userBean.getPassword())){
            System.out.println("登录成功");
            return true;
        }
        System.out.println("登录失败");
        return false;
    }

    /*
    * 方法名：deposit
    * 完成功能：存款功能
    */
    public boolean deposit(MoneyBean account, double money,String fileName) throws InvalidDepositException {
        if(money<0){
            throw new InvalidDepositException("存款金额小于0");
        }
        account.setBalance(money);
        BankDaoInterface bankDaoInterface = BankDaoImpl.getInstance();
//        bankDaoInterface.saveMoney(money,fileName);
        userDao.saveMoney(money,fileName);
        return true;
    }
    /*
     * 方法名：withdrawals
     * 完成功能：取款功能
     */
    public boolean wirhDrawals(MoneyBean account,double money,String fileName) throws AccountOverDrawnException, InvalidDepositException {;
        BankDaoInterface bankDaoInterface = BankDaoImpl.getInstance();
//        double money1 = bankDaoInterface.getMoney(fileName);
        double money1 = userDao.getMoney(fileName);
        account.setBalance(money1);
        if(money>account.getBalance()){
            throw new AccountOverDrawnException("余额不足");
        } else if (money<0) {
            throw new InvalidDepositException("取款金额小于0");
        }


        bankDaoInterface.deleteMoney(money,fileName);
        return true;
    }
    /*
     * 方法名：inquiry
     * 完成功能：查询余额
     */
    public void inquiry(String username,MoneyBean account){
        BankDaoInterface bankDaoInterface = BankDaoImpl.getInstance();
//        account.setBalance(bankDaoInterface.getMoney(username));
        account.setBalance(userDao.getMoney(username));
        System.out.println("您的余额为："+bankDaoInterface.getMoney(username));
    }
    /*
     * 方法名：exitSystem
     * 完成功能：退出系统
     */
    public void exitSystem(MoneyBean moneyBean,UserBean userBean){
        BankDaoInterface bankDaoInterface = BankDaoImpl.getInstance();
//        bankDaoInterface.saveMoney(moneyBean.getBalance(),userBean.getUsername());
        userDao.saveMoney(moneyBean.getBalance(),userBean.getUsername());
        System.exit(0);

    }
    //md5加密
    public String MD5(String str){
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        byte[] md5Bytes = new byte[0];
        try {
            md5Bytes = md5.digest(str.getBytes("UTF8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String md5Str = "";
        for (int i = 0; i < md5Bytes.length; i++) {
            md5Str += Integer.toHexString(md5Bytes[i] | 0xFFFFFF00).substring(6);
        }
        return md5Str;
    }
    //transfer()转账功能
    public boolean transfer(String fileName,double money){
        BankDaoInterface bankDaoInterface = BankDaoImpl.getInstance();
//        bankDaoInterface.addMoney(money,fileName);
        userDao.addMoney(money,fileName);

        return true;

    }


}
